
#include "bsp_A_phase_tim.h"
#include "bsp_gpio_output.h"
#include "bsp_exti.h"
/**
  * @brief  定时器2中断优先级配置
  * @param  无
  * @retval 无
  */
static void PHASE_A_TIM_NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  // 设置中断来源
  NVIC_InitStructure.NVIC_IRQChannel = PHASE_A_TIM_IRQn;
  // 设置抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  定时器2模式配置
  * @param  无
  * @retval 无
  */
static void PHASE_A_TIM_Mode_Config(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  RCC_APB1PeriphClockCmd(PHASE_A_TIM_CLK, ENABLE);

  TIM_TimeBaseStructure.TIM_Prescaler = 48 - 1;
  //定时器分频因子，配置死区时间时需要用到
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  //计数器计数模式，设置为向上计数
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  //重复计数器的值,没有用到不用管
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  // 初始化定时器TIMx, x[2,3,4,5]
  TIM_TimeBaseInit(PHASE_A_TIM, &TIM_TimeBaseStructure);

  // 清除定时器更新中断标志位
  TIM_ClearFlag(PHASE_A_TIM, TIM_FLAG_Update);

  // 开启定时器更新中断
  TIM_ITConfig(PHASE_A_TIM, TIM_IT_Update, ENABLE);

  // 使能定时器
  TIM_Cmd(PHASE_A_TIM, DISABLE);
}

/**
  * @brief  定时器2配置
  * @param  无
  * @retval 无
  */
void Phase_A_Tim_Configuration(void)
{
  PHASE_A_TIM_Mode_Config();
  PHASE_A_TIM_NVIC_Configuration();
}

void PHASE_A_TIM_IRQHandler(void)
{
  if (TIM_GetITStatus(PHASE_A_TIM, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(PHASE_A_TIM, TIM_IT_Update);
    TIM_Cmd(PHASE_A_TIM, DISABLE);
    A_PHASE_INDICATOR_TOGGLE;
    A_OUTPUT(0);
    Delay_us(80);
    A_OUTPUT(1);
  }
}
/*********************************************END OF FILE**********************/